package com.kongmou.Springmvc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kongmou.Springmvc.Entity.DemoObj;

@Controller // 1
@RequestMapping("/anno") // 2
public class DemoAnnoController {

	// 未指定路径，使用方法上的路径
	@RequestMapping(produces = "text/plain;charset=utf-8")
	@ResponseBody
	public String index(HttpServletRequest request) {
		return "url:" + request.getRequestURL() + " can access";
	}

	@RequestMapping(value = "/pathvar/{str}", produces = "text/plain;charset=UTF-8") // 5
	public @ResponseBody String demoPathVar(@PathVariable String str, // 3
			HttpServletRequest request) {
		return "url:" + request.getRequestURL() + " can access,str: " + str;
	}

	@RequestMapping(value = "/requestParam", produces = "text/plain;charset=UTF-8") // 6
	public @ResponseBody String passRequestParam(Long id, HttpServletRequest request) {

		return "url:" + request.getRequestURL() + " can access,id: " + id;

	}

	@RequestMapping(value = "/obj", produces = "application/json;charset=UTF-8") // 7
	@ResponseBody // 8
	public String passObj(DemoObj obj, HttpServletRequest request) {

		return "url:" + request.getRequestURL() + " can access, obj id: " + obj.getId() + " obj name:" + obj.getName();

	}

	// 映射到不同的路径
	@RequestMapping(value = { "/name1", "/name2" }, produces = "text/plain;charset=UTF-8") 
	public @ResponseBody String remove(HttpServletRequest request) {

		return "url:" + request.getRequestURL() + " can access";
	}

}
